/* RMORPG - Sockets.h
   Copyright (C) 2007  Michael Borgmann <code@lechuck.de>
   This code is released under the GNU Public License (GPL)
 */

#ifndef _SOCKET_H
#define _SOCKET_H

#include <sys/socket.h>		// all the socket data structures
#include <netinet/in.h>		// all the functions needed for IPv4 and IPv6
#include <arpa/inet.h>		// all the functions that start with inet_
#include <netdb.h>		// all the needed DNS functions

namespace rmorpg {

	/**
	 * Socket Class
	 */
	class Socket {
		private:
			int sock;
			struct sockaddr_in socketaddress;

			/**
			 * DNS Lookup
			 *
			 * @param *name Name/IP of Host
			 * @return internal host number
			 */
			unsigned long DNSLookup(const char *ip);

		public:
			/**
			 * Default Constructor
			 */
			Socket();

			/**
			 * Default Destructor
			 */
			virtual ~Socket();

			/**
			 * Create TCP Socket
			 *
			 * @return true if succeed
			 */
			bool CreateTCPSocket();

			/**
			 * Create UDP Socket
			 *
			 * @return true if succeed
			 */
			bool CreateUDPSocket();

			/**
			 * Bind Socket
			 *
			 * @param port port number to bind to socket
			 * @return true if succeed
			 */
			bool Bind(const int port, char *addr = 0);

			/**
			 * Listen to Socket
			 *
			 * @param max_connections maximal socket connections
			 * @return true if listening
			 */
			bool Listen(const int max_connections);

			/**
			 * Accept Client
			 *
			 * @param new_socket new data socket
			 * @return true if succeed
			 */
			bool Accept(Socket &new_socket);

			/**
			 * Connect to Server
			 *
			 * @param host host name
			 * @param port port number
			 * @return true if succeed
			 */
			bool Connect(const char *host, int port);

			/**
			 * Send Packet
			 *
			 * @param packet packet to send
			 * @param size size of packet
			 * @return true if succeed
			 */
			bool Send(char *packet, int size);

			/**
			 * Receive Packet
			 *
			 * @param buffer packet buffer
			 * @param size buffer size
			 * @return true if succeed
			 */
			bool Receive(char *buffer, int size);

			/**
			 * Get Socket
			 *
			 * @return socket number
			 */
			int Get();

			/**
			 * Set Socket
			 *
			 * @param socket socket number
			 * @return socket number
			 */
			int Set(int);
	};
}

#endif // end _SOCKET_H
